import unittest
import requests
from api.api_tender import ApiTender
from api.api_register_login import ApiRegisterLogin
from utils import html_parser, read_json
from parameterized import parameterized
from utils import GetLog
log = GetLog.get_logger()


class TestTender(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        log.info('正在执行session初始化--{}'.format(self.session))
        self.tender = ApiTender(self.session)
        ApiRegisterLogin(self.session).api_login()

    def tearDown(self) -> None:
        self.session.close()
        log.info('正在关闭session对象--')

    @parameterized.expand(read_json("tender.json", "tender"))
    def test01_tender(self, amount, expect):
        try:
            if amount != '':
                r = self.tender.api_tender(amount)
                log.info('执行接口结果为：{}'.format(r.text))
                self.assertIn(expect, r.text)
                log.info('执行断言通过')
                result = html_parser(r)
                r = self.session.post(url=result[0], data=result[1])
                log.info('执行接口结果为：{}'.format(r.text))
                self.assertIn('OK', r.text)
                log.info('执行断言通过')
            else:
                r = self.tender.api_tender(amount)
                log.info('执行接口结果为：{}'.format(r.text))

                self.assertIn(expect, r.text)
                log.info('执行断言通过')
        except Exception as e:
            log.error("断言失败，原因为：{}".format(e))
            raise